﻿using System;
using System.Windows;
using System.Windows.Data;

namespace CountJobTags {
    /// <summary>
    /// Returns an arc segment, represented as a drawing string, where the
    /// smile size is proportionate to the input value, which should be
    /// between 0 and 1
    /// </summary>
    public class SmileyConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture) {
            string result = "";
            if (value is double) {
                double theVal = (double)value;
                //Protect ourselves against bad data
                if (theVal > 1.0)
                    theVal = 1.0;
                else if (theVal < 0.0)
                    theVal = 0.0;

                double xInit = 5 - (5.0 * theVal);
                double yInit = 12.5 - (2.5 * theVal);
                double pctHappy = (25 * theVal);
                double xTerm = 7.5 + (5.0 * theVal);
                double yTerm = 12.5 - (2.5 * theVal);
                //Path geometry starts with M x,y, where x,y is the start coordinates
                //ArcSegment has form A rx,ry d f1 f2 x,y F
                //where A      → Start Arc
                //      rx, ry  x and y radius for the ellipse defining the arc segment
                //      d      → degrees to rotate
                //      f1     → flag/IsLargArc
                //      f2     → flag/Clockwise
                //      x,y    → End coordinates of the segment
                //      F      → Optional, close figure
                //bigSmile  = "M 0,10   A 10,25  0 0 0 12.5,10   Z";
                //noSmile   = "M 5,12.5 A 10,0   0 0 0 7.5, 12.5 Z";
                result = string.Format("M {0}, {1} A 10,{2} 0 0 0 {3},{4} Z",
                                       xInit, yInit, pctHappy, xTerm, yTerm);
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture) {
            return DependencyProperty.UnsetValue;
        }
    }
}
